package com.jinxin.platform.cdacsrecord.core.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdacsrecord.core.mapper.JurenAcsControlRecordInfoMapper;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.AcsControlRecordInfo;
import com.jinxin.platform.cdacsrecord.core.service.AcsControlAsyncService;
import com.jinxin.platform.cdacsrecord.core.service.AcsControlDeviceInfoService;
import com.jinxin.platform.base.common.annotation.RecEvent;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.PlatFormEventListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: chenyinlong
 * @Description:
 * @Date: Create in 10:51 2020/4/23
 */
@RestController
@RequestMapping("/unAuth")
@Slf4j
@RecEvent(eventType = "acs-report")
public class externalInterface implements PlatFormEventListener {
    @Autowired
    AcsControlDeviceInfoService acsControlDeviceInfoService;
    @Autowired
    AcsControlAsyncService asyncService;
    @Resource
    private JurenAcsControlRecordInfoMapper recordInfoDao;

    @Override
    public JsonResult event(Map param) {
        log.info("param--"+JSON.toJSONString(param));
        JSONObject jsonObject = JSON.parseObject(param.get("data").toString());
        log.info("report--------"+jsonObject.toString());
        if (jsonObject.get("cmd_name").equals("Password or QR code report")) {
            String resultEntity;
            String mac = jsonObject.get("serial_num").toString();
            JSONObject password = JSONObject.parseObject(jsonObject.get("reportMessage").toString());
            String type = "";
            AcsControlRecordInfo acsControlRecordInfo;
            if (!StringUtils.isEmpty(password.get("r1"))) {
                type = "二维码开门";
                acsControlRecordInfo = recordInfoDao.queryByInfo(password.get("r1").toString() );
                resultEntity = acsControlDeviceInfoService.compare(mac, password.get("r1").toString(),"QR");
            } else {
                type = "随机数字开门";
                acsControlRecordInfo = recordInfoDao.queryByInfo(password.get("r2").toString() );
                resultEntity = acsControlDeviceInfoService.compare(mac, password.get("r2").toString(),"password");
            }
            if(resultEntity.equals("开门成功")){
                asyncService.addReportOpenLog(mac,type,"访客"+acsControlRecordInfo.getVisitorName() );
            }
           acsControlDeviceInfoService.sendMessage(mac,JSON.toJSONString(resultEntity));
        }else if (jsonObject.get("cmd_name").equals("Open door")){
            String mac = jsonObject.get("serial_num").toString();
            String openRes = acsControlDeviceInfoService.openDoor(mac);
            acsControlDeviceInfoService.sendMessage(mac,openRes);
            if(openRes.equals("开门成功")){
                JSONObject msg = JSONObject.parseObject(jsonObject.get("reportMessage").toString());
                asyncService.addReportOpenLog(mac,msg.get("r1")+"",mac);
            }
        }else if (jsonObject.get("cmd_name").equals("Recognition result")){
            JSONObject msg = JSONObject.parseObject(jsonObject.get("reportMessage").toString());
            String mac = jsonObject.get("serial_num").toString();
            if (!StringUtils.isEmpty(msg.get("r1"))) {
                String faceName = (String) msg.get("r1");
                if(faceName.contains("访客")){
                    // 获取访客姓名 设置记录 为已过期 删除设备上的图片
                    log.info("访客人脸识别成功："+ faceName );
                    faceName = faceName.substring(faceName.indexOf("访客")+2);
                    AcsControlRecordInfo acsControlRecordInfo = recordInfoDao.queryByName(faceName);
                    if (!(acsControlRecordInfo == null)) {
                        // 更新邀请码状态为2 已使用
                        if (null == acsControlRecordInfo.getState() || acsControlRecordInfo.getState().equals("1")) {
                            acsControlRecordInfo.setState("2");
                            recordInfoDao.update(acsControlRecordInfo);
                        }
                    }
                }
                asyncService.addReportOpenLog(mac,"人脸识别开门",faceName.substring(faceName.lastIndexOf("-")));
            }
        }
        return JsonResult.ok(externalInterface.class);
    }

}
